/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mwt.xml.xdbforms.schemalayer.impl;

import mwt.xml.xdbforms.dblayer.metadata.impl.ColumnMetaData;
import mwt.xml.xdbforms.schemalayer.types.*;
import mwt.xml.xdbforms.schemalayer.SchemaTypeMapping;
import mwt.xml.xdbforms.schemalayer.types.XmlTypes;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;

/**
 * Progetto Master Web Technology
 * @author Gianfranco Murador, Cristian Castiglia, Matteo Ferri
 */
public class SchemaTypeMappingHandler implements SchemaTypeMapping {

    private XmlTypes typesChain = null;
    private Document document = null;

    public SchemaTypeMappingHandler(Document d) {

        document = d;
        defineTypeChain();

    }

    public SchemaTypeMappingHandler() {
        defineChainXS2J();
    }

    private void defineChainXS2J() {

        typesChain = new XmlChar();


        //Char -> Integer

        XmlTypes integer = new XmlInteger();
        typesChain.setNextType(integer);



        // IL TIPO BOOL VIENE TRATTATO COME UN BIT
        // Integer -> Bool
        //     XmlTypes bool = new XmlBool(document);
        //   integer.setNextType(bool);

        // Integer-> Numeric
        XmlTypes numeric = new XmlDecimal();
        integer.setNextType(numeric);

        // Numeric -> Date
        XmlTypes date = new XmlDate();
        numeric.setNextType(date);

        // Date -> Set
        XmlTypes set = new XmlSet();
        date.setNextType(set);

        // Set -> Enum
        XmlTypes enums = new XmlEnum();
        set.setNextType(enums);

        // Enum -> Bit (BIT tratta pure le i boolean)
        XmlTypes bit = new XmlBit();
        enums.setNextType(bit);


        // BIT -> TEXT
        XmlTypes text = new XmlText();
        bit.setNextType(text);

        // TEXT -> BLOB

        XmlTypes blob = new XmlBlob();
        text.setNextType(blob);

        // BLOB -> Binary
        XmlTypes binary = new XmlBinary();
        blob.setNextType(binary);

    }

    private void defineTypeChain() {


        typesChain = new XmlChar(document);


        //Char -> Integer

        XmlTypes integer = new XmlInteger(document);
        typesChain.setNextType(integer);



        // IL TIPO BOOL VIENE TRATTATO COME UN BIT
        // Integer -> Bool
        //     XmlTypes bool = new XmlBool(document);
        //   integer.setNextType(bool);

        // Integer-> Numeric
        XmlTypes numeric = new XmlDecimal(document);
        integer.setNextType(numeric);

        // Numeric -> Date
        XmlTypes date = new XmlDate(document);
        numeric.setNextType(date);

        // Date -> Set
        XmlTypes set = new XmlSet(document);
        date.setNextType(set);

        // Set -> Enum
        XmlTypes enums = new XmlEnum(document);
        set.setNextType(enums);

        // Enum -> Bit (BIT tratta pure le i boolean)
        XmlTypes bit = new XmlBit(document);
        enums.setNextType(bit);


        // BIT -> TEXT
        XmlTypes text = new XmlText(document);
        bit.setNextType(text);

        // TEXT -> BLOB

        XmlTypes blob = new XmlBlob(document);
        text.setNextType(blob);

        // BLOB -> Binary
        XmlTypes binary = new XmlBinary(document);
        blob.setNextType(binary);

    }

    public DocumentFragment getSchemaType(ColumnMetaData columnMetaData) {
        return typesChain.getSchemaType(columnMetaData);
    }

    /***
     *
     * @param baseType tipoBase di xschema, ad esempio xs:string, xs:integer
     * @param value Valore passato come stringa
     * @return un oggetto base java derivato da Object
     * (Integer, Short, String, etc..)
     */
    public Object getXSchemaToJavaType(String baseType, String value) {
        return typesChain.getXSchemaToJavaType(baseType, value);
       
    }
}
